package com.atguigu.flink0922.chapter11;

import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.table.functions.ScalarFunction;

import static org.apache.flink.table.api.Expressions.$;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/3/12 11:09
 */
public class Flink12_Function_Scalar_2 {
    public static void main(String[] args) {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
    
        DataStreamSource<String> stream = env.fromElements("hello", "atguigu", "Hello");
    
        Table table = tEnv.fromDataStream(stream, $("word"));
       
        // 1. 注册临时函数
        tEnv.createTemporaryFunction("toUpper", ToUpperCase.class);
        // 2. 注册临时表
        tEnv.createTemporaryView("t_word", table);
        // 3. 在临时表上查询使用自定义函数
        tEnv.sqlQuery("select toUpper(word) word_upper from t_word").execute().print();
       
        
    }
    
    public static class ToUpperCase extends ScalarFunction {
        public String eval(String s){
            return s.toUpperCase();
        }
    }
    
    
}



